package com.vcredit.creditcat.service;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.persistence.CustProductApplyExtMapper;
import com.vcredit.creditcat.persistence.base.CustProductApplyMapper;
import com.vcredit.creditcat.util.ConcurrentDateUtil;

/**
 * 
 **/
@Service
public class CustProductApplyService {
	
	public static final String AUTH_ITEM_UNDONE_MSG = "认证项缺失";
	
	@Autowired
	private CustProductApplyMapper mapper;

	@Autowired
	private CustProductApplyExtMapper custProductApplyExtMapper;

	public void insert(CustProductApply obj) {
		mapper.insert(obj);
	}

	public void update(CustProductApply obj) {
		mapper.update(obj);
	}

	public List<CustProductApply> findAll() {
		return mapper.findAll();
	}

	public List<CustProductApply> findByConditions(CustProductApply conditions) {
		return mapper.find(conditions);
	}

	@SuppressWarnings("unused")
	private List<CustProductApply> find(CustProductApply obj) {
		return mapper.find(obj);
	}

	public List<CustProductApply> findByCustId(Long custId) {
		return custProductApplyExtMapper.findByCustId(custId);
	}
	public List<CustProductApply> findByCustNot(Long custId) {
		return custProductApplyExtMapper.findByCustNot(custId);
	}
	public CustProductApply findNewApplyProduct(Long productId, Long custId) {
		return custProductApplyExtMapper.findNewApplyProduct(productId, custId);
	}

	public CustProductApply findByFlowNo(String flowNo) {
		return custProductApplyExtMapper.findByFlowNo(flowNo);
	}

	public List<CustProductApply> findRequireQuryLimitList() {
		return custProductApplyExtMapper.findRequireQuryLimitList();
	}

	public int getSuccessApplyCount(Long custId) {
		return custProductApplyExtMapper.getSuccessApplyCount(custId);
	}

	/**
	 * 校验产品能否申请
	 * 注意：仅供提交申请产品时校验产品是否可以再次申请，当审批状态为22时是可以申请的
	 * 
	 * @author zhaoqi01
	 * @param custId
	 * @param productId
	 * @return
	 * @throws ParseException
	 */
	public boolean checkProductApply(Long custId,
			Long productId) throws ParseException {
		CustProductApply custProductApply = this.findNewApplyProduct(productId,
				custId);
		if(null != custProductApply && custProductApply.getAuditResult() == 22) {
			return true;
		}
		return this.canProductApplyAgin(custProductApply);
	}
	
	/**
	 * 判断用户申请的产品是否可以再次申请
	 * @author zhaoqi01
	 * @param custProductApply
	 * @return
	 * @throws ParseException
     * 0：已审批-失败；1：已审批-成功；2：审批中；3：未申请过；4：未申请过，认证条件齐全；5：额度获取失败临时状态; 7：授信失败，可挽回；
     * 8：放款审核中；9：放款审核未通过；10：放款审核通过；11：还款中；12：还款逾期中；13：已结清；22：快贷流程提交中 
	 */
	public boolean canProductApplyAgin(CustProductApply custProductApply) throws ParseException {
		if(null == custProductApply) {
			return true;
		}
		Integer auditResult = custProductApply.getAuditResult();
		String creditApplyTime = custProductApply.getCreditApplyTime(); // 下次申请时间
		Boolean isApply = custProductApply.getIsApply(); // 是否可以再次申请
		String creditExpiretime = custProductApply.getCreditExpiretime(); // 额度过期时间
		
		Date nowDate= new Date();
		if(auditResult == 1) { // 成功获取到额度
			if(StringUtils.isBlank(creditExpiretime)) {
				// 如果额度过期时间为空，默认不能再次申请
				return false;
			} else {
				Date amtExpireDatetime = ConcurrentDateUtil.strToDate(creditExpiretime);
				// 如果额度已过期，可以再次申请，否则不能再申请
				if(amtExpireDatetime.before(nowDate)) {
					return true;
				} else {
					return false;
				}
			}
		}
		
		else if(auditResult == 0) { // 额度获取失败
			if(isApply) {
				// 可以再次申请需要判断下次申请时间
				if(StringUtils.isBlank(creditApplyTime)) {
					// 下次申请时间为空，默认可以再次申请
					return true;
				} else {
					Date nextApplyTime = ConcurrentDateUtil.strToDate(creditApplyTime);
					// 如果下次申请时间已过，则可以再次申请
					if(nextApplyTime.before(nowDate)) {
						return true;
					} else {
						return false;
					}
				}
				
			} else { // 不能再次申请
				return false;
			}
		} 
		
		else if(auditResult == 3 || auditResult == 4) { // 兼容以前老数据可能出现的3、4这两种状态
			return true;
		}
		
		else { // 其他的都不能再次申请
			return false;
		}
		
	}

	public List<Map<String,Object>> applySuccessList() {
		return mapper.applySuccessList();
	}

	public long findHasResultByCustAndProducts(Long custId,
			List<Long> speedloanProductIds) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("custId", custId);
		params.put("productIdList", speedloanProductIds);
		return mapper.findHasResultByCustAndProducts(params);
	}
	public long findHasResultByCustAndProduct(Long custId,
			List<Long> speedloanProductIds) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("custId", custId);
		params.put("productIdList", speedloanProductIds);
		return mapper.findHasResultByCustAndProduct(params);
	}
	public CustProductApply findByCustAndProduct(Long custId, long productId) {
		return mapper.findByCustAndProduct(custId,productId);
	}

	public List<CustProductApply> findByCustAndAuditResults(Long custId,
			List<Integer> auditResults) {
		return mapper.findByCustAndAuditResults(custId,auditResults);
	}
	
	public String sdGetDeviceID(Long custId) {
		return mapper.sdGetDeviceID(custId);
	}
	public String sdGetCreditAmt(Long custId, long productId) {
		return mapper.sdGetCreditAmt(custId,productId);
	}
	
}
